package org.smart4j.framework.helper;

import org.smart4j.framework.annotation.Controller;
import org.smart4j.framework.annotation.Service;
import org.smart4j.framework.helper.ConfigHelper;
import org.smart4j.framework.utils.ClassUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * @author chen
 * @version V1.0
 * @date 2017/9/27
 */
public class ClassHelper {
    /**
     * 定义类集合，用于存放所加载的类
     */
    private static final Set<Class<?>> CLASS_SET;

    static {
        String basePackage = ConfigHelper.getAppBasePackage();
        CLASS_SET = ClassUtil.getClassSet(basePackage);
    }

    /**
     * 获取包名下所有类
     *
     * @return
     */
    public static Set<Class<?>> getClassSet() {
        return CLASS_SET;
    }

    /**
     * 获取所有service
     */
    public static Set<Class<?>> getServiceClassSet() {
        Set<Class<?>> serviceSet = new HashSet<>();
        for (Class cls : CLASS_SET) {
            if (cls.isAnnotationPresent(Service.class)) {
                serviceSet.add(cls);
            }
        }
        return serviceSet;
    }

    /**
     * 获取所有的controller
     */
    public static Set<Class<?>> getControllerClassSet() {
        Set<Class<?>> serviceSet = new HashSet<>();
        for (Class cls : CLASS_SET) {
            if (cls.isAnnotationPresent(Controller.class)) {
                serviceSet.add(cls);
            }
        }
        return serviceSet;
    }

    /**
     * 获取所有的bean类(包括service，controller)
     */
    public static Set<Class<?>> getBeanClassSet() {
        Set<Class<?>> beanSet = new HashSet<>();
        beanSet.addAll(getClassSet());
        beanSet.addAll(getServiceClassSet());
        return beanSet;
    }
}
